﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.IO.Ports;
using VSSAHT;
using VSS_AHT.Properties;

namespace VSS_AHT
{
    public partial class FormConfigComPort : BaseForm
    {
        #region Declare Availables
        private readonly ParaPort _paraPort = new ParaPort();
        #endregion

        #region Singleton Members
        private static volatile FormConfigComPort instance;
        private static object syncRoot = new Object();

        public static FormConfigComPort Instance
        {
            get
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new FormConfigComPort();
                }

                return instance;
            }
        }
        #endregion

        #region Ctors
        public FormConfigComPort()
        {
            InitializeComponent();
            Initialize();
        }

        public void Initialize()
        {
            this.btnSave.Click += new EventHandler(delegate { ClickButtonOK(); });
            this.btnCancel.Click += new EventHandler(delegate
            {
                this.Close();
            });
            this.Disposed += new EventHandler(delegate { instance = null; });
        }

        #endregion

        #region Methods
        private void LoadPortControl()
        {
            // Ports.
           
            foreach (var str in SerialPort.GetPortNames())
            {
                this.cboPortName.Properties.Items.Add(str.ToString());
            }

            // Parity.
            foreach (string str in Enum.GetNames(typeof(Parity)))
            {
                cboParity.Properties.Items.Add(str);
            }
        }

        public void ClickButtonOK()
        {
            try
            {
                _paraPort.PortName = cboPortName.Text;
                _paraPort.BaudRate = Convert.ToInt32(cboBaudRate.EditValue);
                _paraPort.Parity = cboParity.SelectedIndex;
                _paraPort.DataBits = Convert.ToInt32(cboDataBits.EditValue);

                _paraPort.StartByte = Convert.ToByte(this.txtSTX.Text);
                _paraPort.EndByte = Convert.ToByte(this.txtETX.Text);
                _paraPort.SkipBytes = Convert.ToInt32(txtStep.Text);

                Settings.Default.Save();
                Common.InitPort();
                Common.ShowSuccessful("Lưu cấu hình thành công!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi lưu thông tin cấu hình : " + ex.Message);
            }
        }
        #endregion

        #region Events
        
        protected override void OnLoad(EventArgs e)
        {
            LoadPortControl();
            cboPortName.Text = _paraPort.PortName;
            cboBaudRate.SelectedText = _paraPort.BaudRate.ToString();
            cboParity.SelectedIndex = _paraPort.Parity;
            cboDataBits.SelectedText = _paraPort.DataBits.ToString();
            
            //Using to detect data from ComPort
            txtSTX.Text = _paraPort.StartByte.ToString();
            txtETX.Text = _paraPort.EndByte.ToString();
            txtStep.Text = _paraPort.SkipBytes.ToString();
            base.OnLoad(e);
        }
        protected override void OnClosed(EventArgs e)
        {
            instance = null;
            Settings.Default.Reload();
            base.OnClosed(e);
        }
        #endregion
              
    }
}